package com.moyou.weichat.utils;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.hyphenate.util.EMLog;
import com.hyphenate.util.PathUtil;
import com.moyou.weichat.R;

import java.io.File;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Locale;

import okhttp3.OkHttpClient;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * Created by Administrator on 2017/11/10 0010.
 */

public class ImageUtils {
    private static ImageUtils instance = null;
    public static final int placeholderImage = R.mipmap.home_ic_loading;//加载中图片展位图
    public static final int error = R.mipmap.home_ic_loading;//加载失败图片展位图
    public static final int user_avatar = R.mipmap.say_ic_normal_avatar;//头像的图片展位图

    public static String imageName_str = "upload.jpg";
    public synchronized static ImageUtils getInstance() {
        if (instance == null) {
            instance = new ImageUtils();
        }
        return instance;
    }

    /**
     * 初始化
     *
     * @param context
     */
    public static void initGlide(Context context) {
        Glide.get(context).register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(new OkHttpClient()));
    }

    /**
     * 聊天图片加载
     *
     * @param context
     * @param url
     * @param image
     */
    public static void setChatImage(Context context, String url, final ImageView image) {
        final WeakReference<ImageView> imageViewWeakReference = new WeakReference<>(image);
        ImageView target = imageViewWeakReference.get();
        if (target != null) {
            Glide.with(context.getApplicationContext()).load(url).placeholder(placeholderImage).crossFade()
                    .override(250, 250)
                    .placeholder(placeholderImage)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .error(error).dontAnimate().into(image);
        }
    }


    /**
     * 创建一个文件夹
     */
    public static File establishFile() {
        File Path = new File(Environment.getExternalStorageDirectory()
                + "/upload");
        if (!Path.exists()) {
            Path.mkdir();
        }
        File filePath = new File(Environment.getExternalStorageDirectory()
                + "/upload/", System.currentTimeMillis() + imageName_str);
        return filePath;
    }


    public interface CompressWithBack {
        void getFiles(List<File> fileList);

        void getFile(File file);

        void onError();
    }

}
